a11y: Set an accessible role for GtkExpander
authorMatthias Clasen <mclasen@redhat.com>
Thu, 30 Jul 2020 01:55:34 +0000 (21:55 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Thu, 30 Jul 2020 02:46:00 +0000 (22:46 -0400)
Use the button accessible role for GtkExpander
and set attributes as appropriate.

Update the documentation and add a test.

docs/reference/gtk/section-accessibility.md
gtk/gtkexpander.c
testsuite/a11y/expander.c [new file with mode: 0644]
testsuite/a11y/meson.build

index d4bc80590cd520fd5790ac760c222e6d0cea17b7..4c9b1587326152ffcba86cb9dc984380cde96644 100644 (file)
@@ -46,7 +46,7 @@ Each role name is part of the #GtkAccessibleRole enumeration.
 | Role name | Description | Related GTK widget |
 |-----------|-------------|--------------------|
 | `ALERT` | A message with important information | - |
-| `BUTTON` | A control that performs an action when pressed | #GtkButton, #GtkLinkButton |
+| `BUTTON` | A control that performs an action when pressed | #GtkButton, #GtkLinkButton, #GtkExpander |
 | `CHECKBOX` | A control that has three possible value: `true`, `false`, or `undefined` | #GtkCheckButton |
 | `COLUMNHEADER` | The header of a column in a list or grid | - |
 | `COMBOBOX` | A control that can be expanded to show a list of possible values to select | #GtkComboBox |
index 7993e506d4fcc0edf6665eb0c954918d21c57347..343701c2a8cf30ff183168d050e67d114b93c998 100644 (file)
  * GtkExpander has three CSS nodes, the main node with the name expander,
  * a subnode with name title and node below it with name arrow. The arrow of an
  * expander that is showing its child gets the :checked pseudoclass added to it.
+ *
+ * # Accessibility
+ *
+ * GtkExpander uses the #GTK_ACCESSIBLE_ROLE_BUTTON role.
  */
 
 #include "config.h"
@@ -374,6 +378,7 @@ gtk_expander_class_init (GtkExpanderClass *klass)
                   G_TYPE_NONE, 0);
 
   gtk_widget_class_set_css_name (widget_class, I_("expander-widget"));
+  gtk_widget_class_set_accessible_role (widget_class, GTK_ACCESSIBLE_ROLE_BUTTON);
 }
 
 static void
@@ -420,6 +425,10 @@ gtk_expander_init (GtkExpander *expander)
   gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (gesture),
                                               GTK_PHASE_BUBBLE);
   gtk_widget_add_controller (GTK_WIDGET (expander->title_widget), GTK_EVENT_CONTROLLER (gesture));
+
+  gtk_accessible_update_state (GTK_ACCESSIBLE (expander),
+                               GTK_ACCESSIBLE_STATE_EXPANDED, FALSE,
+                               -1);
 }
 
 static GtkBuildableIface *parent_buildable_iface;
@@ -877,6 +886,10 @@ gtk_expander_set_expanded (GtkExpander *expander,
       gtk_expander_resize_toplevel (expander);
     }
 
+  gtk_accessible_update_state (GTK_ACCESSIBLE (expander),
+                               GTK_ACCESSIBLE_STATE_EXPANDED, expanded,
+                               -1);
+
   g_object_notify (G_OBJECT (expander), "expanded");
 }
 
@@ -1164,6 +1177,8 @@ void
 gtk_expander_set_child (GtkExpander *expander,
                         GtkWidget   *child)
 {
+  GList *list = NULL;
+
   g_return_if_fail (GTK_IS_EXPANDER (expander));
   g_return_if_fail (child == NULL || GTK_IS_WIDGET (child));
 
@@ -1188,6 +1203,13 @@ gtk_expander_set_child (GtkExpander *expander,
         }
     }
 
+  if (expander->child)
+    list = g_list_append (list, expander->child);
+  gtk_accessible_update_relation (GTK_ACCESSIBLE (expander),
+                                  GTK_ACCESSIBLE_RELATION_CONTROLS, list,
+                                  -1);
+  g_list_free (list);
+
   g_object_notify (G_OBJECT (expander), "child");
 }
 
diff --git a/testsuite/a11y/expander.c b/testsuite/a11y/expander.c
new file mode 100644 (file)
index 0000000..169898f
--- /dev/null
@@ -0,0 +1,57 @@
+#include <gtk/gtk.h>
+
+static void
+expander_role (void)
+{
+  GtkWidget *widget = gtk_expander_new ("Hello");
+  g_object_ref_sink (widget);
+
+  gtk_test_accessible_assert_role (widget, GTK_ACCESSIBLE_ROLE_BUTTON);
+
+  g_object_unref (widget);
+}
+
+static void
+expander_state (void)
+{
+  GtkWidget *widget = gtk_expander_new ("Hello");
+  g_object_ref_sink (widget);
+
+  gtk_test_accessible_assert_state (widget, GTK_ACCESSIBLE_STATE_EXPANDED, FALSE);
+
+  gtk_expander_set_expanded (GTK_EXPANDER (widget), TRUE);
+
+  gtk_test_accessible_assert_state (widget, GTK_ACCESSIBLE_STATE_EXPANDED, TRUE);
+
+  g_object_unref (widget);
+}
+
+static void
+expander_relations (void)
+{
+  GtkWidget *widget = gtk_expander_new ("Hello");
+  GtkWidget *child = gtk_label_new ("Child");
+  GList *list;
+
+  g_object_ref_sink (widget);
+
+  gtk_expander_set_child (GTK_EXPANDER (widget), child);
+
+  list = g_list_append (NULL, child);
+  gtk_test_accessible_assert_relation (widget, GTK_ACCESSIBLE_RELATION_CONTROLS, list);
+  g_list_free (list);
+
+  g_object_unref (widget);
+}
+
+int
+main (int argc, char *argv[])
+{
+  gtk_test_init (&argc, &argv, NULL);
+
+  g_test_add_func ("/a11y/expander/role", expander_role);
+  g_test_add_func ("/a11y/expander/state", expander_state);
+  g_test_add_func ("/a11y/expander/relations", expander_relations);
+
+  return g_test_run ();
+}
index 2c25837dbb488f1c46324303608e8992391fc4c7..1244a521f8ca79e198cc54757f3c1fd5304faff0 100644 (file)
@@ -15,6 +15,7 @@ tests = [
   { 'name': 'checkbutton' },
   { 'name': 'dialog' },
   { 'name': 'entry' },
+  { 'name': 'expander' },
   { 'name': 'image' },
   { 'name': 'label' },
   { 'name': 'passwordentry' },